#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# 将文件切分成小段，每次处理完小段，释放内存
def read_in_block(file_path):
    BLOCK_SIZE = 1024  # 每次读取的块大小
    with open(file_path, 'r',encoding='utf-8') as f:
        while True:
            block = f.read(BLOCK_SIZE)
            if block:
                yield block
            else:
                break
# 这个方法，速度很快，但有个问题，若满足了1024时，会将正好在1024位置的数据切开。



#方法2
def read_in_block2(file_path):
    BLOCK_SIZE = 1024  # 每次读取的块大小
    with open(file_path, 'r',encoding='utf-8') as f:
        for line in f:
            # 把每行内容作为生成器的产出值
            yield line

if __name__ == '__main__':
    file_path = r'd:\epean\python_learn\13.文件io\test.txt'
    for block in read_in_block(file_path):
        print(block)
    print("-------------------")
    # 调用修改后的 read_in_block2 函数
    for block in read_in_block2(file_path):
        print(block)